package com.jiusit.onePurchase.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.ProcessBuilder.Redirect;
import java.math.BigDecimal;
import java.math.MathContext;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.imageio.stream.FileImageInputStream;

import org.apache.commons.lang3.RandomUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.jiusit.common.utils.User;
import com.jiusit.common.utils.UserWapper;
import com.jiusit.common.utils.Friendships;
import com.jiusit.common.utils.PostParameter;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.render.Render;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.jfinal.weixin.sdk.kit.IpKit;
import com.jiusit.common.utils.DataCoding;
import com.jiusit.common.utils.DateUtil;
import com.jiusit.common.utils.FileUtil;
import com.jiusit.common.utils.HttpClient;
import com.jiusit.common.utils.Paging;
import com.jiusit.common.utils.Response;
import com.jiusit.common.utils.StringUtil;
import com.jiusit.common.utils.TicketUtils;
import com.jiusit.common.utils.URLEncodeUtils;
import com.jiusit.common.utils.Upload;
import com.jiusit.common.utils.WeiboException;
import com.jiusit.onePurchase.interceptor.UserInterceptor;
import com.jiusit.onePurchase.model.ChampionCompetition;
import com.jiusit.onePurchase.model.ChampionPrizes;
import com.jiusit.onePurchase.model.ChampionUser;
import com.jiusit.onePurchase.model.ChampionUserCurrencyRecord;
import com.jiusit.onePurchase.model.ChampionUserExchang;
import com.jiusit.onePurchase.model.ChampionUserGuessing;
import com.jiusit.onePurchase.model.ChampionUserInvite;
import com.jiusit.onePurchase.model.ChampionUserMessage;
import com.jiusit.onePurchase.model.ChampionUserRun;
import com.jiusit.onePurchase.model.ChampionUserSign;
import com.jiusit.onePurchase.model.WbVar;

@ControllerBind(controllerKey = "/auth", viewPath = "/pages")
@Before(UserInterceptor.class)
/** 
 * 说明： 
 * 
 * @author  王汝绘 
 * @version 创建时间：2016年3月30日 上午11:13:40 
 */
/*
 * 首页
 */
public class AuthorizeController extends ApiController {
	private static final Logger log = Logger.getLogger(AuthorizeController.class);
	HttpClient client = new HttpClient();

	@Override
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();
		HttpClient client = new HttpClient();
		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("client_ID"));
		ac.setAppSecret(PropKit.get("client_SERCRET"));

		/**
		 * 是否对消息进行加密，对应于微信平台的消息加解密方式： 1：true进行加密且必须配置 encodingAesKey
		 * 2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey",
				"setting it in config file"));
		return ac;
	}
    
	public void home() {
		String fromUser = getCookie("fromUser");
		String forwardType = getPara("forwardType");
		String url=getRequest().getRequestURI();
		if(StringUtil.isEmpty(forwardType)){
			forwardType="";
		}
		System.out.println("跳转类型:"+forwardType);
		if(!StringUtil.isEmpty(fromUser)){
			if(!StringUtil.isEmpty(forwardType)){
				redirect("/home/forwardSnatch");
			}else{
				redirect("/home/homePage?fromUser="+fromUser+"&access_token="+getCookie("access_token")+"");
				}
			
		}else{
			setAttr("client_ID", PropKit.get("client_ID"));
			setAttr("redirect_URI", PropKit.get("redirect_URI"));
			setAttr("forwardType", forwardType);
			render("error.html");
		}
	}

	
}
